package com.starling.extend.display
{
	import com.starling.extend.core.PacketDecoder;
	
	import flash.display.BlendMode;
	
	import starling.display.BlendMode;
	import starling.display.DisplayObject;
	import starling.filters.BlurFilter;
	import starling.filters.ColorMatrixFilter;

	public class BDisplay
	{
		public static function setInfo(display:DisplayObject ,info:Object):void
		{
			if(info.textureType == PacketDecoder.TYPE_TEXT)
			{
				display["style"] = info.textInfo;
			}
			
			if(info.textureType != PacketDecoder.TYPE_SCALE9GRID)
			{
				display.scaleX = info.scaleX;
				display.scaleY = info.scaleY;
			}
			
			display.x = info.x;
			display.y = info.y;
			display.width = info.width;
			display.height = info.height;
			display.skewX = info.skewX * PacketDecoder.ANGLE_TO_RADIAN;
			display.skewY = info.skewY * PacketDecoder.ANGLE_TO_RADIAN;
			display.alpha = info.alpha;
			display.name = info.name;
			
			//starling 只能支持单个 滤镜，，暂时只取 数组索引 0 的滤镜
			if(info.filters!=null)
			{
				if(display.filter!=null)
				{
					display.filter.dispose();
					display.filter = null;
				}
				var obj:Object = info.filters[0].value;
				switch(info.filters[0].type)
				{
					case "flash.filters::BlurFilter":
						display.filter = new BlurFilter(obj.blurX ,obj.blurY ,obj.quality/2);
						break;
					
					case "flash.filters::GlowFilter":
						display.filter = BlurFilter.createGlow(obj.color ,obj.alpha ,obj.blurX ,obj.quality/2);
						break;
					
					case "flash.filters::DropShadowFilter":
						display.filter = BlurFilter.createDropShadow(obj.distance ,obj.angle ,obj.color ,obj.alpha ,obj.blurX ,obj.quality/2);
						break;
					
					case "flash.filters::ColorMatrixFilter":
						var matrixVt:Vector.<Number> = new Vector.<Number>();
						for (var m:int=0;m<obj.matrix.length;m++)
							matrixVt.push(obj.matrix[m]);
						display.filter = new ColorMatrixFilter(matrixVt);
						break;
				}
			}
			
			//混合模式
			if(info.blend!=null)
			{
				switch(info.blend)
				{
					case flash.display.BlendMode.LAYER:
						//此模式(图层)会去除图片透明 (优化性能)
						display.blendMode = starling.display.BlendMode.NONE;
						break;
					
					case flash.display.BlendMode.MULTIPLY:
						display.blendMode = starling.display.BlendMode.MULTIPLY;
						break;
					
					case flash.display.BlendMode.SCREEN:
						display.blendMode = starling.display.BlendMode.SCREEN;
						break;
					
					case flash.display.BlendMode.ADD:
						display.blendMode = starling.display.BlendMode.ADD;
						break;
					
					case flash.display.BlendMode.ERASE:
						display.blendMode = starling.display.BlendMode.ERASE;
						break;
				}
			}
			
			BSystem.gc();
		}
	}
}